angular.module('starter.controllers', [])
.controller('AppCtrl', ['$scope', '$ionicModal', '$timeout', '$location', function($scope, $ionicModal, $timeout, $location) {
  // Form data for the login modal
  $scope.loginData = {};

  // Create the login modal that we will use later
  $ionicModal.fromTemplateUrl('templates/login.html', {
    scope: $scope
  }).then(function(modal) {
    $scope.modal = modal;
  });

  // Triggered in the login modal to close it
  $scope.closeLogin = function() {
    $scope.modal.hide();
  };

  // Open the login modal
  $scope.login = function() {
    $scope.modal.show();
  };

  // Perform the login action when the user submits the login form
  $scope.doLogin = function() {
    console.log('Doing login', $scope.loginData);

    // Simulate a login delay. Remove this and replace with your login
    // code if using a login system
    $timeout(function() {
      $scope.closeLogin();
    }, 1000);
  };

  $scope.go = function (state) {
    $location.path(state);
  }
}])

.controller('PlaylistsCtrl', ['$scope', 'ApiService', '$ionicModal', '$location', '$state', function($scope, ApiService, $ionicModal, $location, $state) {

      ApiService.getIndexData(function(data) {
        $scope.playlists = data;
      });

      $ionicModal.fromTemplateUrl('templates/playlist.html', {
        scope: $scope
      }).then(function(modal) {
        $scope.modal = modal;

        $scope.showPlay = function(){
          $scope.modal.show();
        };

        $scope.hidePlay = function() {
          $scope.modal.hide();
        };

        ApiService.getProjectInfo(function(data) {
          $scope.project = data;
        });

        $scope.translatePage = function (state) {
          $scope.hidePlay();
          $state.go('app.projectDetail',{'projectId': state});
        };

      });

      $scope.doRefresh = function(){
        $scope.$broadcast('scroll.refreshComplete');
        ApiService.getProjectInfo(function(data) {
          $scope.project = data;
        }).finally(function(){
          $scope.$broadcast('scroll.refreshComplete');
        });
      }
}])

.controller('PlaylistCtrl', function($scope, $stateParams) {
})

.controller('ChatCtrl', ['$scope', '$stateParams', 'ApiService', function($scope, $stateParams, ApiService){
      ApiService.getChatList(function(data){
          if(data && data.length){
            $scope.chats = data;
          }
      });

      $scope.remove = function(chat){
        console.log(chat)
      };
}])

.controller('ProgressCtrl', ['$scope', '$stateParams', 'ApiService', function ($scope, $stateParams, ApiService) {

    ApiService.getSportInfo( function(data) {
      $scope.sportInfo = data;
    })
}])

.controller('SettingCtrl', ['$scope', '$stateParams', 'ApiService', function ($scope, $stateParams, ApiService) {

  $scope.modalData = {

  };

  $scope.changeData = function() {
    console.log($scope.modalData);
  };

  ApiService.getSettingData(function(data) {
    $scope.settingData = data;
    $scope.modalData.autoplay = data.autoplay;
  })
}])

.controller('ProjectDetailCtrl', ['$scope', '$stateParams', function($scope, $stateParams) {
    $scope.id = $stateParams.projectId;
}])

;
